package sangpil.effects
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import gs.TweenLite;
	import gs.easing.Cubic;
	
	import sangpil.events.GeneralEvent;

	[Event(name = 'changed', type = 'sangpil.events.GeneralEvent')]
	public class CircleArm extends Sprite
	{
		private var radious:Number;
		private var dotColor:uint = 0xA0B7BC;
		private var openSpeed:Number = 0.3;
		private var closeSpeed:Number = 0.3;
		private var startAngle:Number = 0;
		private var arms:Array;
		private var armsAngle:Array;
		private var count:uint;
		private var dotRadious:Number;
		private var menuStrArr:Array;
		public function CircleArm(radious:Number, strArr:Array, subDotRadious:Number = 30)
		{
			super();
			this.radious = radious;
			this.count = strArr.length;
			this.menuStrArr = strArr;
			this.dotRadious = subDotRadious;
			this.init();
		}
		
		private function init():void
		{
			this.arms = new Array();
			this.getPosition();
			this.makeArms();
		}
		
		private function makeArms():void
		{
			for(var i:uint = 0; i < this.count; i++)
			{
				var arm:Sprite = new Sprite();
				this.arms[i] = arm;
				switch(i)
				{
					case 0:
					this.drawCircle(arm, this.dotRadious, 0xAD5240);
					break;
					case 1:
					this.drawCircle(arm, this.dotRadious, 0xffDE26);
					break;
					case 2:
					this.drawCircle(arm, this.dotRadious, 0xE2F0D6);
					break;
					case 3:
					this.drawCircle(arm, this.dotRadious, 0x736859);
					break;
					case 4:
					this.drawCircle(arm, this.dotRadious, 0xE2f0d6);
					break;
					default :
					this.drawCircle(arm, this.dotRadious);
					break;
				}
				arm.alpha = 0;
				arm.rotation = this.startAngle;
				arm.name = this.menuStrArr[i];
				this.addChild(arm);
				arm.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
				arm.addEventListener(MouseEvent.MOUSE_OVER, onOver);
				arm.addEventListener(MouseEvent.MOUSE_OUT, onOut);
			}
		}
		
		private function onDown(e:MouseEvent):void
		{
			this.dispatchEvent(new GeneralEvent(e.target.name, GeneralEvent.CHANGED));
		}
		
		private function onOver(e:MouseEvent):void
		{
			
		}
		
		private function onOut(e:MouseEvent):void
		{
			
		}
		
		
		private function drawCircle(dot:Sprite, baseScale:Number = 30, col:uint = 0xA0B7BC):void
		{
			dot.graphics.beginFill(col);
			dot.graphics.drawCircle(0, this.radious*-1, baseScale);
			dot.graphics.endFill();
		}
		
		private function getPosition():void
		{
			this.armsAngle = new Array();
			var sectedAngle:Number = 360 / this.count;
			for(var i:uint = 0; i < this.count; i++)
			{	
				this.armsAngle[i] = sectedAngle * i;
			}
		}
		
		public function showArm():void
		{
			for(var i:uint = 0; i < this.count; i++)
			{
				TweenLite.to(this.arms[i], this.openSpeed, {rotation:this.armsAngle[i], alpha:1, ease:Cubic.easeInOut});
			}
		}
		
		public function hideArm():void
		{
			for(var i:uint = 0; i < this.count; i++)
			{
				TweenLite.to(this.arms[i], this.closeSpeed, {rotation:this.startAngle, alpha:0, ease:Cubic.easeInOut});
			}
		}
		
	}
}